var authorInfoModel = require('../models/authorInfo');
var bookModel = require('../models/books');
var authorModel = require('../models/author');
var colmunModel = require('../models/colmun')
const search = {
    searchBook: (req, res, next) => {
        if (req.query.search) {
            var key = req.query.search
        } else {
            res.render('search', { loginUser: req.session.userLogin, book: null, key: key })
        }
        authorModel.findOne({ name: { $regex: key } }).then(doc => {
            if (doc) {
                bookModel.find({ author_id: doc._id }).populate('author_id').then(doc2 => {
                    res.render('search', { loginUser: req.session.userLogin, book: doc2, key: key })
                })
            } else {
                bookModel.find({ name: { $regex: key } }).populate('author_id').then(doc => {
                    res.render('search', { loginUser: req.session.userLogin, book: doc, key: key })
                })
            }
        })
    },
}
module.exports = search;